import store from '../../store/index'
import { getUserContactWay } from '../../api/user'
import { sendDatePatApply } from '../../api/publishDatePat'
import { success } from '../../lib/js/utils'

Page(
  store.createPage({
    /**
     * 提交约拍申请
     */
    doSubmit: function() {
      const that = this
      const datePatId = that.data.datePatInfo.id
      const reason = that.data.reason
      sendDatePatApply(datePatId, reason).then(function(res) {
        success('发送成功')
      })
    },
    data: {
      datePatInfo: null,
      // userContactWays: [],
      // 约拍理由
      reason: ''
    },
    globalData: ['contactWays'],
    /**
     * 约拍理由改变触发回调
     */
    changeReason: function(e) {
      const reason = e.detail.value
      this.setData({
        reason: reason
      })
    },
    /**
     * 初始化业务数据
     * @param {*} options
     */
    initData: function() {
      const that = this
      getUserContactWay().then(function(res) {
        that.setData({
          userContactWays: res.data
        })
      })
    },
    onLoad: function(options) {
      const that = this
      that.initData()
      const datePatInfo = JSON.parse(options.datePatInfoStr)
      that.setData({
        datePatInfo: datePatInfo
      })
    }
  })
)
